package com.epsoft.odin7cj.flow.config;

import javax.annotation.Resource;
import javax.sql.DataSource;

//import org.activiti.engine.HistoryService;
//import org.activiti.engine.RepositoryService;
//import org.activiti.engine.RuntimeService;
//import org.activiti.engine.TaskService;
//import org.activiti.spring.ProcessEngineFactoryBean;
//import org.activiti.spring.SpringProcessEngineConfiguration;

import org.camunda.bpm.engine.HistoryService;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.spring.ProcessEngineFactoryBean;
import org.camunda.bpm.engine.spring.SpringProcessEngineConfiguration;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration
public class ActivitiConfig  {
	
    @Resource
    @Qualifier("mainDsDataSource")
    DataSource mainDsDataSource;
    @Resource
    @Qualifier("mainDsTransactionManager")
    PlatformTransactionManager mainDsTransactionManager;

    @Bean
	public SpringProcessEngineConfiguration springProcessEngineConfiguration(){
		SpringProcessEngineConfiguration springProcessEngineConfig = new SpringProcessEngineConfiguration();
		springProcessEngineConfig.setDataSource(mainDsDataSource);
		springProcessEngineConfig.setTransactionManager(mainDsTransactionManager);
//		springProcessEngineConfig.setAsyncExecutorActivate(false);
		// 当数据库已有其他用户创建过ACTIVITI表时，不能再次创建时，使用此选项可以创建表，表可以创建成功，但是会报错，不管他。然后再把该选项注释掉。
//	    springProcessEngineConfig.setDatabaseTablePrefix("ACT_T_");
		springProcessEngineConfig.setDatabaseSchemaUpdate("true");
//		springProcessEngineConfig.setActivityFontName("宋体");
//		springProcessEngineConfig.setLabelFontName("宋体");
		return springProcessEngineConfig;
    }
	
    @Bean
	public ProcessEngineFactoryBean processEngine(){
		ProcessEngineFactoryBean processEngineFactoryBean = new ProcessEngineFactoryBean();
		processEngineFactoryBean.setProcessEngineConfiguration(springProcessEngineConfiguration());
		return processEngineFactoryBean;
	}
	
	@Bean
	public RepositoryService repositoryService() throws Exception{
		return processEngine().getObject().getRepositoryService();
	}
	@Bean
	public RuntimeService runtimeService() throws Exception{
		return processEngine().getObject().getRuntimeService();
	}
	@Bean
	public TaskService taskService() throws Exception{
		return processEngine().getObject().getTaskService();
	}
	@Bean
	public HistoryService historyService() throws Exception{
		return processEngine().getObject().getHistoryService();
	}
	
}
